package com.xuhai.blog.common.exception;

import com.xuhai.blog.common.util.ResultJson;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Author PangJunjie
 * @Date 2022/2/8/008
 */

/*RestControllerAdvice表示通知型控制器，该类中方法都会在指定情况自动发送内容*/
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*监听自定义异常*/
    @ExceptionHandler(RTException.class)
    public ResultJson handleRTException(RTException e){
        return ResultJson.error(e.getCode(),e.getMsg());
    }

    /*监听数据重复异常*/
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultJson handleDuplicateKeyException(){
        return ResultJson.error("数据库已存在该记录");
    }

    /*监听404异常,需要在配置文件中设置静态资源路径才可以生效*/
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResultJson handleNoHandleFoundException(){
        return ResultJson.error(404,"路径不存在，请仔细检查");
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResultJson handleAuthenticationException(){
        return ResultJson.error(401,"tokenshixiao");
    }

    /*监听所有异常,一定写在最下面*/
    @ExceptionHandler(Exception.class)
    public ResultJson handleException(Exception e){
        return ResultJson.error("服务器睡着了...").put("exception",e.getMessage());
    }

}
